using System.Collections.Generic;
using BitBenderGames;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Gameplay.PVE
{
    public class PveTouchController
    {
        private enum ETouchState
        {
            FingerDown,
            Drag,
            None,
        }

        private ETouchState state = ETouchState.None;
        private Vector3 lastTouchPosition;
        
        public void Init()
        {
            state = ETouchState.None;
        }

        public delegate void TouchStart(Vector3 position);

        public delegate void DragStart(Vector3 position);

        public delegate void Drag(Vector3 position);

        public delegate void DragEnd(Vector3 position);

        public delegate void TouchEnd();
        
        public event TouchStart OnTouchStart;

        public event DragStart OnDragStart;

        public event Drag OnDrag;

        public event DragEnd OnDragEnd;

        public event TouchEnd OnTouchEnd;

        private Vector2 lastDragScreenPoint;
        public Vector2 dragVector;
        
        private int touchFingerId;
        
        public void Update()
        {
            dragVector = Vector2.zero;
            bool isEmpty = true;
            if (TouchWrapper.IsFingerDown)
            {
                if (state == ETouchState.None)
                {
                    for (int i = 0; i < TouchWrapper.Touches.Count; i++)
                    {
                        var touch = TouchWrapper.Touches[i];
                        EventSystem eventSystem = EventSystem.current;
                        PointerEventData pointerEventData = new PointerEventData(eventSystem);
                        pointerEventData.position = touch.Position;
                        //射线检测ui
                        List<RaycastResult> uiRaycastResultCache = new List<RaycastResult>();
                        eventSystem.RaycastAll(pointerEventData, uiRaycastResultCache);
                        if (uiRaycastResultCache.Count > 0)
                        {
                            continue;
                        }
                        touchFingerId = touch.FingerId;
                        state = ETouchState.FingerDown;
                        OnTouchStart?.Invoke(touch.Position);
                        isEmpty = false;
                        break;
                    }
                }
                else
                {
                    for (int i = 0; i < TouchWrapper.Touches.Count; i++)
                    {
                        if (TouchWrapper.Touches[i].FingerId == touchFingerId)
                        {
                            var touchPosition = TouchWrapper.Touches[i].Position;
                            if (state == ETouchState.FingerDown)
                            {
                                if (touchPosition != lastTouchPosition)
                                {
                                    lastDragScreenPoint = touchPosition;
                                    OnDragStart?.Invoke(touchPosition);
                                    state = ETouchState.Drag;
                                }
                            }
                            else if (state == ETouchState.Drag)
                            {
                                if (touchPosition != lastTouchPosition)
                                {
                                    dragVector = new Vector2(touchPosition.x,touchPosition.y) - lastDragScreenPoint;
                                    lastDragScreenPoint = touchPosition;
                                    OnDrag?.Invoke(touchPosition);
                                }
                            }
                            isEmpty = false;
                            break;
                        }
                    }
                    
                }
                lastTouchPosition = TouchWrapper.Touch0.Position;
            }
            if (isEmpty)
            {
                if (state == ETouchState.Drag)
                {
                    OnDragEnd?.Invoke(lastTouchPosition);
                }

                if (state != ETouchState.None)
                {
                    OnTouchEnd?.Invoke();
                }
                state = ETouchState.None;
                lastTouchPosition = Vector3.zero;
            }
           
        }
        
    }
}